<?php
namespace BP\Sys;

use BP\DA\DBAccess;
use BP\En\Entities;
use BP\Sys\XML\EnumInfoXml;
use BP\Sys\XML\EnumInfoXmls;

require_once("BP/Web/WebUser.php");
class SysEnums extends Entities {
	public $Num;	// int
	private function __init() { // default class members
		$this->Num = -1;
	}
	public function ToDesc () 
	{
		$strs = "";
		foreach ($this->ToJavaList() as $se) 		{
			$strs += ((($se->getIntKey() . " ") . $se->getLab()) . ";");
		}
		return $strs;
	}
	public function GenerCaseWhenForOracle_String_String_String_String_String_I ($enName, $mTable, $key, $field, $enumKey, $def) // [String enName, String mTable, String key, String field, String enumKey, int def]
	{
		$sql = $Cash->GetObjFormApplication(((((("ESQL" . $enName) . $mTable) . $key) . "_") . $enumKey), NULL);
		if (($sql . NULL))
		{
			return $sql;
		}
		if (($this->size() == 0))
		{
			throw new \RuntimeException((("@枚举�?" . $enumKey) . "已被删除�?"));
		}
		$sql = (((((" CASE NVL(" . $mTable) . $field) . ",") . $def) . ")");
		foreach ($this->ToJavaList() as $se1) 		{
			$sql += ((((" WHEN " . $se1->getIntKey()) . " THEN '") . $se1->getLab()) . "'");
		}
		$se = $this->GetEntityByKey(SysEnumAttr::$IntKey, $def);
		if (($se == NULL))
		{
			$sql += ((" END \"" . $key) . "Text\"");
		}
		else
		{
			$sql += ((((" WHEN NULL THEN '" . $se->getLab()) . "' END \"") . $key) . "Text\"");
		}
		$Cash->AddObj(((((("ESQL" . $enName) . $mTable) . $key) . "_") . $enumKey), $Depositary->Application, $sql);
		return $sql;
	}
	public function GenerCaseWhenForOracle_String_String_String_String_I ($mTable, $key, $field, $enumKey, $def) // [String mTable, String key, String field, String enumKey, int def]
	{
		if (($this->size() == 0))
		{
			throw new \RuntimeException((("@枚举值（" . $enumKey) . "）已被删除，无法形成期望的SQL�?"));
		}
		$sql = "";
		$sql = ((" CASE " . $mTable) . $field);
		foreach ($this->ToJavaList() as $se1) 		{
			$sql += ((((" WHEN " . $se1->getIntKey()) . " THEN '") . $se1->getLab()) . "'");
		}
		$se = $this->GetEntityByKey(SysEnumAttr::$IntKey, $def);
		if (($se == NULL))
		{
			$sql += ((" END \"" . $key) . "Text\"");
		}
		else
		{
			$sql += ((((" WHEN NULL THEN '" . $se->getLab()) . "' END \"") . $key) . "Text\"");
		}
		return $sql;
	}
	public function LoadIt ($enumKey) // [String enumKey]
	{
		if (($this->Full($enumKey) ==  FALSE ))
		{
			try 
			{
				DBAccess::RunSQL((("UPDATE Sys_Enum SET Lang='" . $WebUser->getSysLang()) . "' WHERE LANG IS NULL "));
				DBAccess::RunSQL("UPDATE Sys_Enum SET MyPK=EnumKey+'_'+Lang+'_'+cast(IntKey as NVARCHAR )");
				$dbUrl = new DBUrl();
				if (($DBType->MSSQL == $dbUrl->getDBType()))
				{
					DBAccess::RunSQL("UPDATE Sys_Enum SET MyPK=EnumKey+'_'+Lang+'_'+cast(IntKey as NVARCHAR )");
				}
				else
					if (($DBType->Oracle == $dbUrl->getDBType()))
					{
						DBAccess::RunSQL("UPDATE Sys_Enum SET MyPK = EnumKey || '_' || Lang || '_' || cast(IntKey  as VARCHAR(5))");
					}
					else
						if (($DBType->MySQL == $dbUrl->getDBType()))
						{
							DBAccess::RunSQL("UPDATE Sys_Enum SET MyPK = CONCAT (EnumKey,'_', Lang,'_',CAST(IntKey AS CHAR(5)))");
						}
			}
			catch (\Exception $e)
			{ /* empty */ }
			try 
			{
				$xml = EnumInfoXml::constructor__String($enumKey);
				$this->RegIt($enumKey, $xml->getVals());
			}
			catch (\RuntimeException $ex)
			{
				throw new \RuntimeException(((("@你没有预制[" . $enumKey) . "]枚举值�?�@在修复枚举�?�出现错�?:") . $ex->getMessage()));
			}
		}
	}
	public static function RegAll () 
	{
		$xmls = EnumInfoXmls::constructor__();
		$xmls->RetrieveAll();
		$ses = SysEnums::constructor__();
		foreach ($xmls->ToJavaList() as $xml) 		{
			$ses->RegIt($xml->getKey(), $xml->getVals());
		}
	}
	public static function constructor__String ($enumKey) // [String enumKey]
	{
		$me = new self();
		$me->__init();
		$me->LoadIt($enumKey);
		return $me;
	}
	public static function constructor__String_String ($enumKey, $vals) // [String enumKey, String vals]
	{
		$me = new self();
		$me->__init();
		if ((($vals . NULL) . $vals->equals("")))
		{
			$me->LoadIt($enumKey);
			return ;
		}
		if (($me->Full($enumKey) ==  FALSE ))
		{
			$me->RegIt($enumKey, $vals);
		}
		return $me;
	}
	public function RegIt ($EnumKey, $vals) // [String EnumKey, String vals]
	{
		try 
		{
			$strs = $vals->split("[@]", -1);
			$ens = SysEnums::constructor__();
			$ens->Delete(SysEnumAttr::$EnumKey, $EnumKey);
			$this->clear();
			foreach ($strs as $s) 			{
				if ((($s == NULL) || $s->equals("")))
				{
					continue;
				}
				$vk = $s->split("[=]", -1);
				$se = SysEnum::constructor__();
				$se->setIntKey($Integer->parseInt($vk[0]));
				$kvsValues = array();
				for ($i = 0; ($i < count($kvsValues) /*from: kvsValues.length*/); ++$i) 
				{
					$kvsValues[$i] = $vk[($i + 1)];
				}
				$se->setLab($BP->Tools->StringHelper->join("=", $kvsValues));
				$se->setEnumKey($EnumKey);
				$se->setLang($WebUser->getSysLang());
				$se->Insert();
				$this->AddEntity($se);
			}
		}
		catch (\RuntimeException $ex)
		{
			throw new \RuntimeException((($ex->getMessage() . " - ") . $vals));
		}
	}
	public function Full ($enumKey) // [String enumKey]
	{
		$ens = $Cash->GetObjFormApplication((("EnumOf" . $enumKey) . $WebUser->getSysLang()), NULL);
		if (($ens != NULL))
		{
			$this->AddEntities($ens);
			return  TRUE ;
		}
		$qo = new QueryObject($this);
		$qo->AddWhere(SysEnumAttr::$EnumKey, $enumKey);
		$qo->addAnd();
		$qo->AddWhere(SysEnumAttr::$Lang, $WebUser->getSysLang());
		$qo->addOrderBy(SysEnumAttr::$IntKey);
		if (($qo->DoQuery() == 0))
		{
			return  FALSE ;
		}
		$Cash->AddObj((("EnumOf" . $enumKey) . $WebUser->getSysLang()), $Depositary->Application, $this);
		return  TRUE ;
	}
	public function Delete ($key, $val) // [String key, Object val]
	{
		try 
		{
			$en = $this->getNewEntity();
			$ps = new Paras();
			$ps->SQL = (((((("DELETE FROM " . $en->getEnMap()->getPhysicsTable()) . " WHERE ") . $key) . "=") . $en->getHisDBVarStr()) . "p");
			$ps->Add("p", $val);
			return $en->RunSQL($ps);
		}
		catch (\Exception $e)
		{
			$en = $this->getNewEntity();
			$en->CheckPhysicsTable();
			$ps = new Paras();
			$ps->SQL = (((((("DELETE FROM " . $en->getEnMap()->getPhysicsTable()) . " WHERE ") . $key) . "=") . $en->getHisDBVarStr()) . "p");
			$ps->Add("p", $val);
			return $en->RunSQL($ps);
		}
	}
	public static function constructor__ () 
	{
		$me = new self();
		$me->__init();
		return $me;
	}
	public function getNewEntity () 
	{
		return SysEnum::constructor__();
	}
	public function GetLabByVal ($val) // [int val]
	{
		foreach ($this->ToJavaList() as $en) 		{
			if (($en->getIntKey() == $val))
			{
				return $en->getLab();
			}
		}
		return NULL;
	}
	public function ToJavaList () 
	{
		return $this;
	}
	public function Tolist () 
	{
		$list = [];// 
		for ($i = 0; ($i < $this->size()); ++$i) 
		{
			$list->add($this->get($i));
		}
		return $list;
	}
}
?>
